package com.free.food.order.convert;

import com.alibaba.fastjson.JSONArray;
import com.free.food.core.enums.ResultEnum;
import com.free.food.core.exception.OrderException;
import com.free.food.order.domain.dto.OrderDTO;
import com.free.food.order.domain.request.CreateOrderRequest;
import com.free.food.order.entity.OrderDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 类功能描述：
 *
 * @author：刘富国
 * @createTime：2019/2/14 13:47
 */
public class CreateOrderRequest2OrderDTO {

    private static final Logger log = LoggerFactory.getLogger(CreateOrderRequest2OrderDTO.class);

    public static OrderDTO convert(CreateOrderRequest request) {
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setBuyerName(request.getName());
        orderDTO.setBuyerAddress(request.getAddress());
        orderDTO.setBuyerOpenid(request.getOpenid());
        orderDTO.setBuyerPhone(request.getPhone());
        List<OrderDetail> orderDetails = null;
        try {
            orderDetails = JSONArray.parseArray(request.getItems(), OrderDetail.class);
        } catch (Exception e) {
            log.error("【json转换】错误，string={}", request.getItems());
            throw new OrderException(ResultEnum.PARAM_ERROR);
        }
        orderDTO.setOrderDetails(orderDetails);
        return orderDTO;
    }
}
